package com.zhang.wanx.service.impl;

import com.obs.services.ObsClient;
import com.obs.services.exception.ObsException;
import com.obs.services.model.PutObjectResult;
import com.zhang.wanx.common.ObsConfig;
import com.zhang.wanx.service.IHuaWeiYunService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;

/**
 * description
 *
 * @author zhangFanJun
 * @date 2024-03-26 17:47
 **/
@RequiredArgsConstructor
@Slf4j
@Service
public class HuaWeiYunService implements IHuaWeiYunService {

    private final ObsClient obsClient;

    @Override
    public synchronized String fileUpload(MultipartFile file) {

        try {
            PutObjectResult res = obsClient.putObject(ObsConfig.bucketName, "wanx"+File.separator+file.getOriginalFilename(), file.getInputStream());
            log.info("文件上传结果：{}",res);
            return res.getObjectUrl();
        } catch (ObsException e) {
            log.error("文件上传失败：",e);
        } catch (Exception e) {
            log.error("文件上传其他异常：",e);
        }
        return null;
    }

}
